package com.xyazm.core.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyazm.core.domain.SegmentDO;
import com.xyazm.core.mapper.SegmentMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 号段表 DAO
 * </p>
 *
 */
@Repository
public class SegmentDAO extends BaseDAO<SegmentMapper, SegmentDO> {

    /**
     * 获取所有bizTag
     * @author xuyou
     * 2022-12-31 19:59
    */
    public List<String> listAllBizTag() {
        return baseMapper.listAllBizTag();
    }

    /**
     * 更新号段最大值，并获取最新OrderAutoNoDO
     * @author xuyou
     * 2022-12-31 20:59
     * @param bizTag
    */
    @Transactional(rollbackFor = Exception.class)
    public SegmentDO updateMaxIdAndGet(String bizTag) {
        // 更新maxId = 当前maxId + 步长
        int ret = baseMapper.updateMaxId(bizTag);
        if (ret != 1) {
            throw new RuntimeException("段号申请失败");
        }
        QueryWrapper<SegmentDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SegmentDO::getBizTag,bizTag);
        return baseMapper.selectOne(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    public SegmentDO updateMaxIdByDynamicStepAndGet(String bizTag, int dynamicStep) {
        int ret = baseMapper.updateMaxIdByDynamicStepAndGet(bizTag,dynamicStep);
        if (ret != 1) {
            throw new RuntimeException("段号申请失败");
        }
        QueryWrapper<SegmentDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SegmentDO::getBizTag,bizTag);
        return baseMapper.selectOne(queryWrapper);
    }
}
